﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FanPages.Server.Services.Contracts;
using FanPages.Server.Services;
using FanPages.Model;
using FanPages.Client.Core.Transformers;
using FanPages.Common;
using FanPages.Server.Services.Contracts.Dto;

namespace FanPagesWeb.Controllers
{
    public class FixedLinkController : Controller
    {
        IFixedLinkService service = new FixedLinkService();
        ITransformer<FixedLink,FixedLinkDto> transformer = new FixedLinkTransformer();

        public ActionResult Index()
        {
            var fanItem = Session["FanItem"] as FanItem;
            var list = service.GetByFanItem(new FanItemTransformer().ToDto(fanItem));
            return View(transformer.ToListEntity(list));
        }


        public ActionResult Details(int id)
        {
            return View(transformer.ToClient(service.GetById(id)));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FixedLink fixedLink)
        {
            try
            {
                fixedLink.FanItem = Session["FanItem"] as FanItem;
                service.Save(transformer.ToDto(fixedLink));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            return View(transformer.ToClient(service.GetById(id)));
        }

        [HttpPost]
        public ActionResult Edit(int id, FixedLink fixedLink)
        {
            try
            {
                fixedLink.FanItem = Session["FanItem"] as FanItem;
                service.Save(transformer.ToDto(fixedLink));                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            return View(transformer.ToClient(service.GetById(id)));
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                service.Delete(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
